﻿@model  Model.ModelConfig
@{
    ViewBag.Title = "发送模型配置";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <script type="text/javascript">
        var UrlArray = $.getUrlArray();
        var modelTypeList = new Array();
        var opts = {
            rows: 15,
            height: 450,
            para: { dbName: encodeURI(UrlArray["dbName"]), version: UrlArray["version"] },
            idField: "GUID",
            columns: [
                    { field: 'DefaultView', title: '名称', width: 40 },
                    { field: 'TableDescription', title: '描述', width: 40 },
                    { field: 'CreateTime', title: '创建时间', width: 10, align: 'center' },
                    { field: 'GUID', title: '操作', width: 10, align: 'center', formatter: function (value, rowData, rowIndex) {
                        var add = true; ;
                        for (var j = 0; j < modelTypeList.length; j++) {
                            if (modelTypeList[j].GUID == rowData.GUID) {
                                add = false;
                            }
                        }
                        if (add) {
                            modelTypeList.push(rowData);
                        }
                        return "<a href='#' onclick='remove(\"" + rowIndex + "\")'>删除</a>";
                    }
                    }
                    ]
        };
        $(function () {
            $.post('/SysMenu/DBList', { dbType: '1' }, function (result) {
                for (var i = 0; i < result.length; i++) {
                    $("#sel_DBName").append("<option value='" + result[i].DBName + "'>" + result[i].DisplayName + "</option>");
                }
            });
        });
        function AddItem() {
            showModalMany('Contact', '/ModelConfig/ModelList?type=1&selectCount=-1&dbName=' + encodeURI($("#sel_DBName").val()), '800', '480', returnFun);
        }
        function remove(i) {
            if (confirm("确定要删除吗？")) {
                modelTypeList.removeAt(i);
                var data = { "total": 0, "rows": modelTypeList };
                opts["data"] = data;
                $("#DataGrid").dg(opts);
            }
        }
        function returnFun(v) {
            for (var i = 0; i < v.length; i++) {
                var has = false;
                for (var j = 0; j < modelTypeList.length; j++) {
                    if (v[i].GUID == modelTypeList[j].GUID) {
                        has = true;
                        break;
                    }
                }
                if (has == false) {
                    modelTypeList.push(v[i]);
                }
            }
            var data = { "total": 1, "rows": modelTypeList };
            opts["data"] = data;
            $("#DataGrid").dg(opts);
        }
        function back() {
            window.location = "/ModelConfig/Index?dbName=" + encodeURI(UrlArray["dbName"]);
        }
        function save(n) {
            if ($.trim($("#txtSysName").val()) == "") {
                alert("新源系统名称不能为空。");
                return;
            }
            if ($.trim($("#txtTableName").val()) == "") {
                alert("新模型名称不能为空。");
                return;
            }
            if (UrlArray["dbName"] == $("#sel_DBName").val()) {
                alert("不能发送到本实例。");
                return;
            }
            var typeIDs = new Array()
            for (var j = 0; j < modelTypeList.length; j++) {
                typeIDs.push(modelTypeList[j].GUID);
            }
            var para = {
                SysName: $("#txtSysName").val(),
                ModelName: $("#txtTableName").val(),
                Description: $("#txtDescription").val(),
                SendDB: $("#sel_DBName").val(),
                IsRecord: 0,
                ModelTypeID: typeIDs.join(","),
                ModelRefID: UrlArray["modelID"],
                dbName: encodeURI(UrlArray["dbName"])
            };
            $("#loading").show();
            $.post("/ModelConfig/SendModelConfig", para, function (result) {
                if (result[0] == "ok") {
                    if (n == "save") {
                        alert("操作成功！");
                    }
                    else
                        window.location = "/ODSConfig/ODSConfigAdd?modelID=" + result[1] + "&dbName=" + UrlArray["dbName"] + "&nmID=" + result[2] + "&ndbName=" + encodeURI($("#sel_DBName").val());
                }
                else if (result[0] == "alert") {
                    alert(result[1]);
                }
                else
                    $("#msg").html(result);
                $("#loading").hide();
            });
        }
    </script>
}
<div style="margin: 5px;">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="300">
                发送模型配置到实例
            </td>
            <td align="right">
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td align="right" width="120">
                原模型名称：
            </td>
            <td>
                @Model.TableName
            </td>
        </tr>
        <tr>
            <td align="right">
                原模型描述：
            </td>
            <td>
                @Model.TableDescription
            </td>
        </tr>
        <tr>
            <td align="right">
                选择实例：
            </td>
            <td>
                <select id="sel_DBName">
                </select>
            </td>
        </tr>
        <tr>
            <td align="right">
                <span class="red">*</span>新源系统名称：
            </td>
            <td>
                <input type="text" id="txtSysName" style="width: 350px;" />
            </td>
        </tr>
        <tr>
            <td align="right">
                <span class="red">*</span>新模型名称：
            </td>
            <td>
                <input type="text" id="txtTableName" style="width: 350px;" />
            </td>
        </tr>
        <tr>
            <td align="right">
                新模型描述：
            </td>
            <td>
                <textarea id="txtDescription" style="width: 350px; height: 50px;"></textarea>
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td valign="top" height="300">
                <table class="listTitle">
                    <tr>
                        <td width="15">
                            <img src="/images/edit.png" />
                        </td>
                        <td width="300">
                            新模型标签
                        </td>
                        <td align="right">
                            <div class="modelEdit">
                                <input type="button" value="添加标签" onclick="AddItem();" class="button" />
                            </div>
                        </td>
                    </tr>
                </table>
                <table id="DataGrid" class="pagTable" cellpadding="0" cellspacing="0">
                </table>
            </td>
        </tr>
    </table>
    <input type="button" class="button" value="返回" onclick="back()" /> 
    <input type="button" class="button" value="下一步" onclick="save('')" />
    <div id="msg">
    </div>
    <div id="loading" class="loading">
        <img src="/Images/tree_loading.gif" />
    </div>
</div>
